package com.pan.entity;

import com.pan.vo.AdminRegisterVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Arrays;


@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Admin {
    Long id;
    String name;
    String password;
    String salt;
    String gender;
    Timestamp birthday;
    Long tel;
    String email;
    Timestamp registerDate;
    Timestamp lastLoginDate;
    Long loginCount;
    Long createId;
    Integer adminStatus;

    public Admin(AdminRegisterVo adminRegisterVo,String password,String salt,Long createId){

        Field[] fields = adminRegisterVo.getClass().getDeclaredFields();
        Arrays.stream(fields).forEach(field -> {
            String memberName = field.getName();
            Class memberType = field.getType();
            String firstUp = memberName.substring(0,1).toUpperCase()+memberName.substring(1);
            String methodGet = "get"+firstUp;
            String methodSet = "set"+firstUp;
            Method getMethod;
            Method setMethod;
            try {
                setMethod = Admin.class.getMethod(methodSet,memberType);
                getMethod = adminRegisterVo.getClass().getMethod(methodGet);
                Object value = getMethod.invoke(adminRegisterVo);
                setMethod.invoke(this ,value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        this.password = password;
        this.salt = salt;
        this.createId = createId;
        this.adminStatus = 0;
        this.loginCount = 0L;
        Timestamp nowTime = new Timestamp(System.currentTimeMillis());
        this.registerDate = nowTime;
        this.lastLoginDate = nowTime;
    }
}